<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

class ProviderController extends AdminController {
    public function providerlist(){
        $list = $this->lists("Provider");
        $this->assign("_list",$list);
        $this->display();
    }
    public function newprovider(){
        if(IS_POST){
            $name = I('providername');
            $pwd = I('providerpwd');
            $account = I('provideraccount');
            $tel = I('tel');
            $permissionArr = I('permission');
            $permissionStr = "";
            for($i = 0;$i<count($permissionArr);$i++){
                $permissionStr.= $permissionArr[$i].",";
            }
            $check['account'] = $account;
            $checkRes = M('Provider')->where($check)->find();
            if($checkRes){
                $this->error("账号已存在");
                return false;
            }
            $data['name'] = $name;
            $data['pwd'] = $pwd;
            $data['account'] = $account;
            $data['permission'] =$permissionStr;
            $data['createdAt'] =time();
            $data['updatedAt'] =time();
            $data['tel'] =$tel;
            if(M("Provider")->add($data)){
                $this->success("新增成功",U("providerlist"));
            }
        }else{
            $prolist = M('Product')->select();
            $this->assign("_prolist",$prolist);
            $this->display();
        }
    }
    public function editprovider($id=""){
        if(IS_POST){
            $id = I('id');
            $name = I('providername');
            $pwd = I('providerpwd');
            $account = I('provideraccount');
            $tel = I('tel');
            $permissionArr = I('permission');
            $permissionStr = "";
            for($i = 0;$i<count($permissionArr);$i++){
                $permissionStr.= $permissionArr[$i].",";
            }
            $map['id'] = $id;
            $data['name'] = $name;
            $data['pwd'] = $pwd;
            $data['account'] = $account;
            $data['permission'] =$permissionStr;
            $data['updatedAt'] =time();
            $data['tel'] =$tel;
            if(M("Provider")->where($map)->save($data)){
                $this->success("修改成功",U("providerlist"));
            }
        }else{
            $map['id'] = $id;
            $info = M('Provider')->where($map)->find();
            $permissonList = explode(",",$info['permission']);
            array_splice($permissonList,count($permissonList)-1,1);
            $prolist = M('Product')->select();
            $this->assign("_prolist",$prolist);
            $this->assign("info",$info);
            $this->permissionlist=$permissonList;
            $this->display();
        }

    }

}
